/*
 * Amkai Enterprise Solution (AES.) 
 * Copyright 2004 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler.dictionary
 * File: IsContractSetForPlanPoliciesHandler.java
 * Created: 2010.04.29. 13:40:00
 * Author: Keri Kornel (keri.kornel)
 * 
 */

/**
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler.dictionary;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.amkai.aes.aescommon.contract.ContractManager;
import com.amkai.aes.aescommon.service.binary.job.dictionary.IsContractSetForPlanPoliciesJob;
import com.amkai.aes.aescommon.service.result.dictionary.IsContractSetForPlanPoliciesResult;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

/**
 * @author KeriKornel
 *
 */
// KKORNEL: AES-10934
public class IsContractSetForPlanPoliciesHandler implements JobObjectHandler<IsContractSetForPlanPoliciesJob, Serializable, IsContractSetForPlanPoliciesResult> {

	@SuppressWarnings("unchecked")
	@Override
	public IsContractSetForPlanPoliciesResult process(IsContractSetForPlanPoliciesJob job, Serializable object) throws Exception {
		IsContractSetForPlanPoliciesResult result = new IsContractSetForPlanPoliciesResult();
		if ((job.getInsurancePlanId() != null) && (job.getContractId() != null)) {
			SessionManager sm = SiteManager.Start();
			try {
				// KKORNEL: AES-10934, AES-13621
				boolean contractSetForPolicy = false;
				Object[] contractDates = 
					(Object[])sm.execute("SELECT effectiveDate, expirationDate FROM Contract WHERE id = :contractId")
				                .setLong("contractId", job.getContractId()).uniqueResult();
				if ((contractDates != null) && (contractDates[0] != null) && (contractDates[1] != null)) {
					List<Object[]> bgiDatesList = 
						sm.execute("SELECT ip.effectiveDate, ip.expirationDate, pmc.dateOfService " +
								   "FROM BillingGroupInsurance bgi " +
								   "JOIN bgi.insurancePolicy ip " +
								   "JOIN ip.insurancePlan iplan " +
								   "JOIN iplan.contracts cont " +
								   "JOIN bgi.billingGroup bg " +
								   "JOIN bg.pmCases pmc " +
								   "WHERE (iplan.id = :iplanId) " +
								   "AND (cont.id = :contractId) " +
								   "AND (pmc.created = (SELECT MAX(pmc2.created) FROM PmCase pmc2 JOIN pmc2.billingGroups bg2 WHERE (bg2.id = bg.id)))")
						  .setLong("iplanId", job.getInsurancePlanId())
						  .setLong("contractId", job.getContractId()).list();
					if (bgiDatesList != null) {
						for (Object[] bgiDate : bgiDatesList) {
							Date applyDate = ContractManager.determineApplyDateForContractSearching((Date)bgiDate[0], (Date)bgiDate[1], (Date)bgiDate[2]);
							if ((applyDate.compareTo((Date)contractDates[0]) >= 0) && (applyDate.compareTo((Date)contractDates[1]) <= 0)) {
								contractSetForPolicy = true;
								break;
							}
						}
					}
				}
				result.setContractSetForPolicy(contractSetForPolicy);
			} finally {
				sm.closeSession();
			}
		}
		return result;
	}

}
